package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.Doctor;
import com.qf.service.DoctorService;
import com.qf.util.AliOssUtil;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * (Doctor)表控制层
 *
 * @author ZhiHuiJun
 * @since 2024-05-02 16:06:57
 */
@RestController
@RequestMapping("/api/doctor")
public class DoctorController {
    /**
     * 服务对象
     */
    @Autowired
    private DoctorService doctorService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @GetMapping("/list")
    public R queryByPage(Long pageNum) {
        return R.ok(this.doctorService.queryByPage(pageNum));
    }

    @RequestMapping("/list/page")
    public R selectDoctorByCondition(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.parseInt((String) map.get("pageNum"));
        int pageSize = Integer.parseInt((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);

        List<Doctor> list = doctorService.selectDoctorByCondition(map);
        if (list != null && !list.isEmpty()) {
            PageInfo<Doctor> pageInfo = new PageInfo<>(list);
            return R.ok(pageInfo);
        }

        return R.fail("查询失败");
    }

    @RequestMapping("/selectAllDoctor")
    public List<Doctor> selectAllDoctor() {
        return doctorService.selectAllDoctor();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Long id) {
        return R.ok(this.doctorService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param doctor 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(Doctor doctor) {
        boolean result = doctorService.insert(doctor);
        if (result) {
            return R.ok();
        }
        return R.fail();
    }

    /**
     * 编辑数据
     *
     * @param doctor 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Doctor doctor) {
        boolean result = doctorService.update(doctor);
        if (result) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param doctorId 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{doctorId}")
    public R deleteById(@PathVariable("doctorId") Long doctorId) {
        boolean result = doctorService.deleteById(doctorId);
        if (result) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer> list) {
        boolean result = doctorService.deleteBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail("批量删除失败");
    }

    /**
     * 导出数据
     * @param response
     */
    @RequestMapping("/doctorExport")
    public void doctorExport(HttpServletResponse response) {
        doctorService.doctorExport(response);
    }

    /**
     * 导入文件
     * @param file
     * @return
     */
    @RequestMapping("/importDoctor")
    public R importDoctor(MultipartFile file) {
        boolean result = doctorService.importDoctor(file);
        if (result) {
            return R.ok();
        }
        return R.fail("导入失败");
    }

    @RequestMapping("/tx/upload")
    public R uploadTx(@RequestPart MultipartFile img, Long doctorId) throws IOException {

        if (img!=null){
            //上传图片
            String url = AliOssUtil.uploadByte("java-2401", img.getOriginalFilename(), img.getBytes());

            //根据doctorId 查询医生更新医生
            Doctor doctor = doctorService.queryById(doctorId);

            if (doctor!=null){
                doctor.setAvatar(url);
                boolean result = doctorService.update(doctor);
                if (result){
                    return R.ok();
                }
            }
        }

        return R.fail();
    }

}

